import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import { CudTableService } from '../../../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../../../public/serve/remove-table/remove-table.service';
import { PaginationComponent } from '../../../../../public/pagination/pagination.component';

import { appProductUrl } from '../../../../product.url';
import { template } from '../../../../product.data';
import {Service} from '../../../../../server/server';

declare var $: any;

@Component({
  selector: 'app-template',
  templateUrl: './template.component.html',
  styleUrls: ['./template.component.css']
})
export class TemplateComponent implements OnInit {

    pageNo: number = 1;
    pageSize: number = 20;
    total: number = 0;
    naturalNum: number = 0;
    list: Array<object>;

    queryCriteria: string = '';

    @Input() selectedTemplateNames: string;     //选中的模板名称
    @Input() selectedTemplateIds: string;     //选中的模板id
    @Input() taskName: string;                      //任务名称
    @Input() templateId:any; //编辑已选的模板id
    @Output() templateNames = new EventEmitter<string>();
    @Output() templateIds = new EventEmitter<string>();
    @ViewChild('pagination') pagination: PaginationComponent;

    constructor(
        private server:Service,
        private request: CudTableService,
        private hintsService: HintsService,
        public removeTableService: RemoveTableService) { }


    ngOnInit() {
    }

    getTaskMetaDataTemplateList() {
        this._cancelChecked();
        let params = this._tableData();
        this.request.postRequest(this.server.baseUrl +"Service/Config/getMetaDataTemplateList", params).then((res: any) => {
            if (res.result_code === '0000') {
                this.list = res.result;
                this.total = res.length;
                this.naturalNum = res.length;
            } else {
                this.hintsService.messages(res.message, res.result_code);
            }
            return;
        })
            .catch((err: any) => {
                this.hintsService.messages("error", "error");
                return;
            });
    }

    //查询参数
  private _tableData(){
    return {
      token         : template.retrieval.token,
      pageNo        : this.pageNo,
      pageSize      : this.pageSize,
      status        : 0,
      queryCriteria : this.queryCriteria
    }
  }

    /**
     * 单选
     * @param child
     * @param length
     */
    _chose(child, length) {
        this.selectedTemplateNames = child.templateName;
        this.selectedTemplateIds = child.templateId;
        this.removeTableService.removeAll(false, '', '');
        this.removeTableService.removeChose(true, child, length)['bool'];
    }

    /**
     * 重置表格单选框、选中的ID
     */
    public _cancelChecked() {
        this.list = [];
    }

    /**
     * 关闭弹出窗口
     */
    public dismissModal() {
        this.templateNames.emit(this.selectedTemplateNames);
        this.templateIds.emit(this.selectedTemplateIds.toString());
        $("#taskTemplateModel").modal('hide');
    }

    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        this.queryCriteria = val.trim();
        this.queryCriteria = val.trim();
        this.getTaskMetaDataTemplateList();
    }
    /**
      * 分页
      * @param pagination 
      */
    _pagination(pagination: Array<any>) {
        this.pageNo = pagination[0].pageNumber;
        this.pageSize = pagination[0].pageSize;
        this.naturalNum = (this.pageNo - 1) * this.pageSize;
        this.getTaskMetaDataTemplateList();
    }

}
